<template>
  <el-form
    ref="dialogForm"
    :model="dialogForm"
    label-width="120px"
    :rules="dialogRules"
  >
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dialogForm.username" placeholder="请输入用户名" />
    </el-form-item>
    <el-form-item v-if="!dialogForm.id" label="登录密码" prop="password">
      <el-input v-model="dialogForm.password" placeholder="请输入登录密码" />
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dialogForm.mobile" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="是否启用" prop="isEnable">
      <el-select
        v-model="dialogForm.isEnable"
        placeholder="请选择是否启用"
      >
        <el-option
          v-for="item in enumsJson.IS_ENABLE"
          :key="item.code"
          :label="item.label"
          :value="item.code"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item class="operate-btns">
      <el-button size="small" @click="closeModel">取消</el-button>
      <el-button
        size="small"
        :loading="loading"
        type="primary"
        @click="onSubmit('dialogForm')"
        >确认</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script>
import { handleConfirm } from "@/utils/messageBox";
import { userManage } from "@/api/system";
import { mapState } from "vuex";

export default {
  props: {
    currentRow: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      loading: false,
      dialogRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入正确的手机号", trigger: "blur" },
        ],
        isEnable: [
          { required: true, message: "请选择是否启用", trigger: "change" },
        ],
      },
      dialogForm: {
        username: "",
        password: "",
        mobile: "",
        isEnable: null,
      },
    };
  },
  watch: {
    currentRow: {
      handler(val) {
        if (val) {
          let { id, username, password, mobile, isEnable } = val;
          this.dialogForm = { id, username, password, mobile, isEnable };
        }
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {
    ...mapState({
      enumsJson: (state) => state.enums.enums,
    }),
  },
  created() {},
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isAdd();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    isAdd() {
      let msg = this.dialogForm.id ? "编辑" : "新增";
      handleConfirm(`确定要${msg}人员信息吗？`, "warning", "提示")
        .then((res) => {
          this.doAdd();
        })
        .catch((err) => {
          // do something cancel
        });
    },
    doAdd() {
      this.loading = true;
      let url = this.dialogForm.id ? "/update" : "/insert";
      let params={...this.dialogForm};
      if(params.id){delete params.password}
      userManage(url, "post", params)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            this.$refs["dialogForm"].resetFields();
            this.$emit("cancelDialog", true);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    closeModel() {
      this.$emit("cancelDialog", false);
    },
  },
};
</script>
<style scoped lang="scss">
.operate-btns {
  text-align: right;
  margin-top: 20px;
}
.el-select {
  width: 100%;
}
</style>
